package com.epfresh.pickerview.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.res.AssetManager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.epfresh.supplier.entity.City;
import com.epfresh.supplier.entity.CityAddressList;
import com.epfresh.pickerview.view.WheelOptions;
import com.epfresh.supplier.R;
import com.epfresh.api.utils.LocalDisplay;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

/**
 * Created by Administrator on 2016/5/17.
 */
public class WheelDialog<T> extends Dialog implements View.OnClickListener {


    WheelOptions wheelOptions;
    private View btnSubmit, btnCancel;
    private TextView tvTitle;
    private OnOptionsSelectListener optionsSelectListener;
    private static final String TAG_SUBMIT = "submit";
    private static final String TAG_CANCEL = "cancel";
    private Context context;
    private Object tag;

    public WheelDialog(Context context) {
        this(context, R.style.ActionSheetDialogStyle);

    }

    public WheelDialog(Context context, int themeResId) {
        super(context, themeResId);
        this.context = context;

        init();
    }

    private void init() {

        View view = LayoutInflater.from(context).inflate(R.layout.pickerview_options, null);

        setContentView(view);
        Window dialogWindow = getWindow();
        dialogWindow.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = LocalDisplay.SCREEN_WIDTH_PIXELS;
        dialogWindow.setAttributes(lp);


        // -----确定和取消按钮
        btnSubmit = findViewById(R.id.btnSubmit);
        btnSubmit.setTag(TAG_SUBMIT);
        btnCancel = findViewById(R.id.btnCancel);
        btnCancel.setTag(TAG_CANCEL);
        btnSubmit.setOnClickListener(this);
        btnCancel.setOnClickListener(this);
        //顶部标题
        tvTitle = (TextView) findViewById(R.id.tvTitle);
        // ----转轮
        final View optionspicker = findViewById(R.id.optionspicker);
        wheelOptions = new WheelOptions(optionspicker);
    }


    public Object getTag() {
        return tag;
    }

    public void setTag(Object tag) {
        this.tag = tag;
    }

    public void setPicker(ArrayList<T> optionsItems) {
        wheelOptions.setPicker(optionsItems, null, null, false);
    }

    public void setPicker(ArrayList<T> options1Items,
                          ArrayList<ArrayList<T>> options2Items, boolean linkage) {
        wheelOptions.setPicker(options1Items, options2Items, null, linkage);
    }

    public <T> void setPicker(ArrayList<T> options1Items,
                              ArrayList<ArrayList<T>> options2Items,
                              ArrayList<ArrayList<ArrayList<T>>> options3Items,
                              boolean linkage) {
        wheelOptions.setPicker(options1Items, options2Items, options3Items,
                linkage);
    }

    /**
     * 设置选中的item位置
     *
     * @param option1
     */
    public void setSelectOptions(int option1) {
        wheelOptions.setCurrentItems(option1, 0, 0);
    }

    /**
     * 设置选中的item位置
     *
     * @param option1
     * @param option2
     */
    public void setSelectOptions(int option1, int option2) {
        wheelOptions.setCurrentItems(option1, option2, 0);
    }

    /**
     * 设置选中的item位置
     *
     * @param option1
     * @param option2
     * @param option3
     */
    public void setSelectOptions(int option1, int option2, int option3) {
        wheelOptions.setCurrentItems(option1, option2, option3);
    }

    /**
     * 设置选项的单位
     *
     * @param label1
     */
    public void setLabels(String label1) {
        wheelOptions.setLabels(label1, null, null);
    }

    /**
     * 设置选项的单位
     *
     * @param label1
     * @param label2
     */
    public void setLabels(String label1, String label2) {
        wheelOptions.setLabels(label1, label2, null);
    }

    /**
     * 设置选项的单位
     *
     * @param label1
     * @param label2
     * @param label3
     */
    public void setLabels(String label1, String label2, String label3) {
        wheelOptions.setLabels(label1, label2, label3);
    }

    /**
     * 设置是否循环滚动
     *
     * @param cyclic
     */
    public void setCyclic(boolean cyclic) {
        wheelOptions.setCyclic(cyclic);
    }

    public void setCyclic(boolean cyclic1, boolean cyclic2, boolean cyclic3) {
        wheelOptions.setCyclic(cyclic1, cyclic2, cyclic3);
    }


    @Override
    public void onClick(View v) {
        String tag = (String) v.getTag();
        if (tag.equals(TAG_CANCEL)) {
            dismiss();
            return;
        } else {
            if (optionsSelectListener != null) {
                int[] optionsCurrentItems = wheelOptions.getCurrentItems();
                optionsSelectListener.onOptionsSelect(optionsCurrentItems[0], optionsCurrentItems[1], optionsCurrentItems[2]);
            }
            dismiss();
            return;
        }
    }

    public interface OnOptionsSelectListener {
        public void onOptionsSelect(int options1, int option2, int options3);
    }

    public void setOnoptionsSelectListener(
            OnOptionsSelectListener optionsSelectListener) {
        this.optionsSelectListener = optionsSelectListener;
    }

    public void setTitle(String title) {
        tvTitle.setText(title);
    }

    public CityAddressList getOptions1(int index) {
        CityAddressList cityAddressList = null;
        if (addressList != null && addressList.size() > index) {
            cityAddressList = addressList.get(index);
        }
        return cityAddressList;
    }

    public CityAddressList getOptions2(int index, int index2) {
        CityAddressList cityAddressList = null;
        if (childrenLists2 != null && childrenLists2.size() > index) {
            ArrayList<CityAddressList> cityAddressLists = childrenLists2.get(index);
            if (cityAddressLists != null && cityAddressLists.size() > index2) {
                cityAddressList = cityAddressLists.get(index2);
            }
        }
        return cityAddressList;
    }

    public CityAddressList getOptions3(int index, int index2, int index3) {
        CityAddressList cityAddressList = null;
        if (childrenLists3 != null && childrenLists3.size() > index) {
            ArrayList<ArrayList<CityAddressList>> arrayLists = childrenLists3.get(index);

            if (arrayLists != null && arrayLists.size() > index2) {
                ArrayList<CityAddressList> cityAddressLists = arrayLists.get(index2);

                if (cityAddressLists != null && cityAddressLists.size() > index3) {
                    cityAddressList = cityAddressLists.get(index3);

                }
            }
        }
        return cityAddressList;
    }

    ArrayList<CityAddressList> addressList;
    ArrayList<ArrayList<CityAddressList>> childrenLists2;
    ArrayList<ArrayList<ArrayList<CityAddressList>>> childrenLists3;

    public void setCity(City cityData) {

        if (cityData != null) {
            addressList = cityData.getAddressList();

            childrenLists2 = new ArrayList<>();
            childrenLists3 = new ArrayList<>();

            if (addressList != null && addressList.size() > 0) {
                for (int i = 0; i < addressList.size(); i++) {
                    CityAddressList cityAddressList = addressList.get(i);
                    ArrayList<CityAddressList> children = cityAddressList.getChildren();
                    childrenLists2.add(children);
                    ArrayList<ArrayList<CityAddressList>> childrenlist = new ArrayList<>();
                    for (int j = 0; j < children.size(); j++) {
                        CityAddressList cityAddressListChildren = children.get(j);
                        ArrayList<CityAddressList> children1 = cityAddressListChildren.getChildren();
                        childrenlist.add(children1);
                    }
                    childrenLists3.add(childrenlist);
                }

                setPicker(addressList, childrenLists2, childrenLists3, true);
                wheelOptions.setFirst(true);
                wheelOptions.setTail(true);
            }
        }

    }

    public void setCurrentIndex(String provinceId, String cityId, String districtId) {
        if (provinceId != null && cityId != null && districtId != null
                && !"".equals(provinceId)
                && !"".equals(cityId)
                && !"".equals(districtId)) {
            int i1 = 0;
            int i2 = 0;
            int i3 = 0;
            for (int i = 0; i < addressList.size(); i++) {
                CityAddressList cityAddressList = addressList.get(i);
                String code = cityAddressList.getCode();
                if (provinceId.equals(code)) {
                    i1 = i;
                    ArrayList<CityAddressList> children = cityAddressList.getChildren();

                    for (int j=0;j<children.size();j++){
                        CityAddressList cityAddressList1 = children.get(j);
                        if (cityId.equals(cityAddressList1.getCode())){
                            i2 = j;

                            ArrayList<CityAddressList> children1 = cityAddressList1.getChildren();

                            for (int k=0;k<children1.size();k++){
                                CityAddressList cityAddressList2 = children1.get(k);
                                if (districtId.equals(cityAddressList2.getCode())){
                                    i3=k;
                                }
                            }
                        }
                    }

                }
            }
            setSelectOptions(i1,i2,i3);
        }
    }

    public City getCityData() {
        String cityDataStr = "";
        City city = null;
        AssetManager asset = context.getAssets();
        InputStream input = null;
        try {
            input = asset.open("epfresh_area_data");
            BufferedReader bufferedReader = new BufferedReader(
                    new InputStreamReader(input));
            StringBuilder stringBuilder = new StringBuilder();
            while ((cityDataStr = bufferedReader.readLine()) != null) {
                stringBuilder.append(cityDataStr);
            }
            bufferedReader.close();
            input.close();
            cityDataStr = stringBuilder.toString();//创建超长string
            if (cityDataStr != null && !"".equals(cityDataStr)) {

                JSONObject jsonObject = new JSONObject(cityDataStr);

                String response = jsonObject.getString("response");

                city = new Gson().fromJson(response.trim(), City.class);

            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return city;
    }
}
